var app = angular.module('ma-forcebuilder.filters', []);

// Walk through the unit's damage values, and return them as a string
app.filter('augSummary', function() {
    function getS(value) {
        if (value === undefined || value === 0) { return "-"; }
        else { return value + "+"; }
    }

    return function(unit) {
        var retVal = "",
            atkS = getS(unit.attack),
            defS = getS(unit.defense),
            ewS = getS(unit.ew);

        return atkS + " / " + defS + " / " + ewS;
    }
});

// Walk through the unit's damage values, and return them as a string
app.filter('damageNumbers', function() {
    function getDamS(value) {
        if (value === undefined || value === 0) { return "-"; }
        else { return value; }
    }

    return function(unit) {
        var retVal = "",
            ud = unit['damage'],
            structureS = getDamS(ud['structure']),
            crippledS = getDamS(ud['crippled']),
            weaponS = getDamS(ud['weapon']),
            movementS = getDamS(ud['movement']),
            electronicS = getDamS(ud['electronic']);

        if (ud['structure'] || ud['crippled']) {
            // Simple damage
            retVal = structureS + " / " + crippledS;
        } else {
            retVal = weaponS + " / " + movementS + " / " + electronicS
        }

        return retVal;
    }
});

// Walk through the unit's damage values, and return them as a string
app.filter('damageCircles', function() {
    function getDamS(value) {
        var boxChar = 'O';
        if (value === undefined || value === 0) { return "-"; }
        else { return Array(value + 1).join(boxChar); }
    }

    return function(unit) {
        var retVal = "",
            ud = unit['damage'],
            structureS = getDamS(ud['structure']),
            crippledS = getDamS(ud['crippled']),
            weaponS = getDamS(ud['weapon']),
            movementS = getDamS(ud['movement']),
            electronicS = getDamS(ud['electronic']);

        if (ud['structure'] || ud['crippled']) {
            // Simple damage
            retVal = structureS + " / " + crippledS;
        } else {
            retVal = weaponS + " / " + movementS + " / " + electronicS
        }

        return retVal;
    }
});

// Walk through the unit weapons, return the list of them in a short-format
app.filter('shortWeapons', function() {
    return function(weapons) {
        if (weapons === undefined) { return ""; }

        var length = weapons.length,
            weaponS = "",
            idx, weapon;

        for (idx = 0; idx < length; idx++) {
            weapon = weapons[idx];
            if (weaponS != "") {
                weaponS += ", ";
            }
            weaponS += weapon['name'] + "-" + weapon['power'] + "";
        }

        return weaponS;
    }
});

// Walk through the unit triats, return the list of them in a short-format
app.filter('shortTraits', function() {
    return function(traits) {
        if (traits === undefined) { return ""; }

        var length= traits.length,
            traitS = "",
            idx, trait;

        for (idx = 0; idx < length; idx++) {
            trait = traits[idx];
            if (traitS != "") {
                traitS += ", ";
            }
            traitS += trait['name'];
            if (trait['rating'] !== undefined) {
                traitS += ":" + trait['rating'];
            }
        }


        return traitS;
    }
});